package com.youzikj.wx.pay;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
import java.util.UUID;

import com.youzikj.wx.pay.hongbao.MoneyUtils;

public class JsConfig {
	
	 private String url;
     private String jsapi_ticket;
     private String nonceStr;
     private String timestamp;
     private String signature;
     
     
     
     /**
       * 算出签名
       * @param jsapi_ticket 
       * @param url 业务中调用微信js的地址
       * @return
       */
      public static JsConfig sign(String jsapi_ticket, String url) {
    	  String nonce = create_nonce_str();
    	  String timeStamp = create_timestamp();
    	  String string1;
          String signature = "";

          //注意这里参数名必须全部小写，且必须有序
          string1 = "jsapi_ticket=" + jsapi_ticket +
                    "&noncestr=" + nonce +
                    "&timestamp=" + timeStamp +
                    "&url=" + url;
        try
        {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(string1.getBytes("UTF-8"));
            signature = byteToStr(crypt.digest());
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        JsConfig result = new JsConfig();
          result.setUrl(url);
          result.setJsapi_ticket(jsapi_ticket);
          result.setNonceStr(nonce);
          result.setTimestamp(timeStamp);
          result.setSignature(signature);

          return result;
      }
       
  	public String getUrl() {
  		return url;
  	}
  	public void setUrl(String url) {
  		this.url = url;
  	}
  	public String getJsapi_ticket() {
  		return jsapi_ticket;
  	}
  	public void setJsapi_ticket(String jsapi_ticket) {
  		this.jsapi_ticket = jsapi_ticket;
  	}
  	public String getNonceStr() {
  		return nonceStr;
  	}
  	public void setNonceStr(String nonceStr) {
  		this.nonceStr = nonceStr;
  	}
  	public String getTimestamp() {
  		return timestamp;
  	}
  	public void setTimestamp(String timestamp) {
  		this.timestamp = timestamp;
  	}
  	public String getSignature() {
  		return signature;
  	}
  	public void setSignature(String signature) {
  		this.signature = signature;
  	}
   
  	
  	 /**
  	     * 将字节数组转换为十六进制字符串
  	     * 
  	     * @param byteArray
  	     * @return
  	      */
    public static String byteToStr(byte[] byteArray) {
    	   Formatter formatter = new Formatter();  
           for (byte b : byteArray)  
           {  
               formatter.format("%02x", b);  
           }  
           String result = formatter.toString();  
           formatter.close();  
           return result;  
       }
    
    private static String create_nonce_str() {  
        return UUID.randomUUID().toString();  
    }  
    
    private static String create_timestamp() {  
        return Long.toString(System.currentTimeMillis() / 1000);  
    }  
    
    

}
